<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 基础选项管理ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysItemMultilevel.class.php');

class ajax_baseItem {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1://显示第二，三级
                    $this->addTheSecondLevel();
                    break;

                case 6://显示第四级
                    $this->addTheThirdLevel();
                    break;

                case 2://隐藏下级
                    $this->removeTheSecondLevel();
                    break;

                case 101://删除选项
                    $this->deleteItemMultilevel();
                    break;

                case 4://获取第一级
                    $this->getTheFirstLevel();
                    break;

                case 100://保存 编辑
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit', 'change'))) {
                        returnJsonData('参数错误');
                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->saveItemMultilevel();//保存 编辑

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteItemMultilevel();//删除选项

                    }

                    break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {
            $this->toReturn('参数错误');
        }


    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    //显示第二级
    private function addTheSecondLevel(){

        global $G_PGLOB;

        $edit_permission = $G_PGLOB->has_permission(30);

        $sysItemMultilevel = new sysItemMultilevel($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $arg_array['sortname'] = 'sort_order';

        $arg_array['sortorder'] = 'ASC';

        //判断点击大分类还是中分类
        if(isset($_REQUEST['mid']) && $_REQUEST['mid'] == 'mid'){

            $arg_array['parent_id'] = $_REQUEST['cate_id'];//中分类ID

            $result = $sysItemMultilevel->getItemMultilevelList($arg_array);

            if($result){

                $mid = 'sid';
                $str = '';
                //组织HTML
                foreach($result as $val){

                    $cat_str = $val['item_id'];
                    $aid = 1;

                    if($val['hasMid']){
                        $img = '<div id="img_add_1" onclick="category.add_cate('.$val['item_id'].',\''.$mid.'\')" class="left closeImg"></div>';
                    }else{
                        $img =  '<div class="left childImg" ></div>';
                    }

                    $str  .= '<tr id="scate'.$val['item_id'].'">';
                    $str  .= '<td style="text-align:left;">
                    <div style = "float:left;margin-left:50px;width:300px">
                    <div style = "float:left" id="tdone_s_'.$val['item_id'].'">'.$img.'</div>
                        <div id = "s_cell1_'.$val['item_id'].'" style="text-align:left;">'.$val['item_name'].'
                        </div>
                    </div>
                    </div>
                    </td>';
                    $str  .= '<td  class = "tac">' . $val['sort_order'] . '</td>';

                    if($edit_permission){
                        $str  .= '<td style="text-align:center">
                        <div class="childCategory" onclick="category.addChild(\'s\',\''.$cat_str.'\',\'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="editCategory" onclick = "category.add(\'s\',\''.$cat_str.'\',\''.$aid.'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="delCategory" onclick = "category.catedelete(\'s\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                    }
                    $str  .= '</tr>';
                }

                $str_arr = array('tr'=>$str);

            }


        }else{

            $arg_array['parent_id'] = $_REQUEST['cate_id'];

            $result = $sysItemMultilevel->getItemMultilevelList($arg_array);

            if($result){

                $mid = 'mid';
                $str = '';
                //组织HTML
                foreach($result as $val){

                    $cat_str = $val['item_id'];

                    $aid = 1;

                    if($val['hasMid']){
                        $img = '<div id="img_add_1" onclick="category.add_cate('.$val['item_id'].',\''.$mid.'\')" class="left closeImg"></div>';
                    }else{
                        $img =  '<div class="left childImg" ></div>';
                    }

                    $str  .= '<tr id="mcate'.$val['item_id'].'">';
                    $str  .= '<td style="text-align:left;">
                    <div style = "float:left;margin-left:25px;width:200px">
                        <div style = "float:left" id="tdone_m_'.$val['item_id'].'">'.$img.'</div>
                        <div id = "m_cell1_'.$val['item_id'].'" style="text-align:left;">'.$val['item_name'].'
                        </div>
                    </div>
                    </td>';
                    $str  .= '<td  class = "tac">' . $val['sort_order'] . '</td>';
                    if($edit_permission){
                        $str  .= '<td style="text-align:center">
                        <div class="childCategory" onclick="category.addChild(\'m\',\''.$cat_str.'\',\'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="editCategory" onclick = "category.add(\'m\',\''.$cat_str.'\',\''.$aid.'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="delCategory" onclick = "category.catedelete(\'m\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                    }
                    $str  .= '</tr>';
                }

                $str_arr = array('tr'=>$str);
            }

        }

        echo json_encode($str_arr);
    }

    //显示第三级
    private function addTheThirdLevel(){

        global $G_PGLOB;

        $edit_permission = $G_PGLOB->has_permission(30);

        $sysItemMultilevel = new sysItemMultilevel($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $arg_array['sortname'] = 'sort_order';

        $arg_array['sortorder'] = 'ASC';

        //判断点击大分类还是中分类
        if(isset($_REQUEST['mid']) && $_REQUEST['mid'] == 'sid'){

            $arg_array['parent_id'] = $_REQUEST['cate_id'];//中分类ID

            $result = $sysItemMultilevel->getItemMultilevelList($arg_array);

            if($result){

                $str = '';
                //组织HTML
                foreach($result as $val){

                    $cat_str = $val['item_id'];
                    $aid = 1;

                    $str  .= '<tr id="lcate'.$val['item_id'].'">';
                    $str  .= '<td style="text-align:left;">
                    <div style = "float:left;margin-left:75px;width:300px">
                    <div id="tdone_l_'.$val['item_id'].'" style="float:left">
                      <div class="left childImg" ></div>
                    </div>

                        <div id = "l_cell1_'.$val['item_id'].'" style="text-align:left;">'.$val['item_name'].'
                        </div>
                    </div>
                    </td>';
                    $str  .= '<td  class = "tac">' . $val['sort_order'] . '</td>';

                    if($edit_permission){
                        $str  .= '<td style="text-align:center">
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="editCategory" onclick = "category.add(\'l\',\''.$cat_str.'\',\''.$aid.'\')" style="margin-left: 46px;"></div>
                        </td>';
                        $str  .= '<td style="text-align:center">
                        <div class="delCategory" onclick = "category.catedelete(\'l\',\''.$cat_str.'\')" style="margin-left: 46px;"></div>
                        </td>';
                    }
                    $str  .= '</tr>';
                }

                $str_arr = array('tr'=>$str);

            }


        }

        echo json_encode($str_arr);

    }

    //隐藏下级
    private function removeTheSecondLevel(){

        global $G_PGLOB;

        $sysItemMultilevel = new sysItemMultilevel($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        //判断是点击大分类移除还是小分类移除
        if($_REQUEST['mid'] == ''){

            $arg_array['parent_id'] = $_REQUEST['cate_id'];

            //ID数组
            $idArr = array();

            $result = $sysItemMultilevel->searchMidRegion($arg_array);

            if($result){

                foreach($result as $val){

                    $idArr[] = $val['item_id'];

                    $searchSmallArr = array(
                        'parent_id'=>$val['item_id']
                    );

                    $smallResult = $sysItemMultilevel->searchMidRegion($searchSmallArr);

                    if($smallResult){

                        foreach($smallResult as $smallVal){
                            $idArr[] =  $smallVal['item_id'];

                            $searchLittleArr = array(
                                'parent_id'=>$smallVal['item_id']
                            );

                            $littleResult = $sysItemMultilevel->searchMidRegion($searchLittleArr);

                            if($littleResult){
                                foreach($littleResult as $littleVal){
                                    $idArr[] =  $littleVal['item_id'];
                                }
                            }

                        }

                    }

                }
            }

        }else if($arg_array['mid'] == 'mid'){
            $arg_array['parent_id'] = $_REQUEST['cate_id'];

            //ID数组
            $idArr = array();

            $result = $sysItemMultilevel->searchMidRegion($arg_array);

            if($result){

                foreach($result as $val){

                    $idArr[] = $val['item_id'];

                    $searchSmallArr = array(
                        'parent_id'=>$val['item_id']
                    );

                    $smallResult = $sysItemMultilevel->searchMidRegion($searchSmallArr);

                    if($smallResult){

                        foreach($smallResult as $smallVal){
                            $idArr[] =  $smallVal['item_id'];

                        }

                    }

                }
            }
        }else{

            $arg_array['parent_id'] = $_REQUEST['cate_id'];

            //中分类ID数组
            $idArr = array();

            $result = $sysItemMultilevel->searchMidRegion($arg_array);

            if($result){

                foreach($result as $val){

                    $idArr[] =  $val['item_id'];
                }

            }

        }
        //misArr :中分类ID数组 smlArr:小分类ID数组
        echo json_encode(array('idArr'=>$idArr));

    }

    //删除选项
    private function deleteItemMultilevel(){

        global $G_PGLOB;

        $sysItemMultilevel = new sysItemMultilevel($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $result = $sysItemMultilevel->deleteItemMultilevel($arg_array);

        echo json_encode($result);

    }

    //获取第一级
    private function getTheFirstLevel(){

        global $G_PGLOB;

        $sysItemMultilevel = new sysItemMultilevel($G_PGLOB->DBCA);

        $arg_array['parent_id'] = 0;

        $result_data = $sysItemMultilevel->getItemMultilevelList($arg_array);

        //编辑信息
        if(isset($_REQUEST['editStr']) && !empty($_REQUEST['editStr'])){

            $editStr = $_REQUEST['editStr'];

            $bigid_arr = array(
                "item_id" => $editStr
            );

            $result = $sysItemMultilevel->getItemMultilevelList($bigid_arr);


            if($result){
                $result_data['editData'] = $result;
            }

        }

        if(!$result_data){
            $result_data = array('emsg'=>1);
        }

        echo json_encode($result_data);

    }



    //保存 编辑
    private function saveItemMultilevel(){

        global $G_PGLOB;

        $sysItemMultilevel = new sysItemMultilevel($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $arg_array['creator'] = $G_PGLOB->currentUserID;

        switch($_REQUEST['editcate']){
            case 'b'://保存 编辑大分类
                $result = $sysItemMultilevel->saveFirstLevel($arg_array);
                break;
            case 'm'://保存 编辑中分类
                $result = $sysItemMultilevel->saveSecondLevel($arg_array);
                break;
            case 's'://保存 编辑小分类
                $result = $sysItemMultilevel->saveThirdLevel($arg_array);
                break;
            case 'l'://保存 编辑第四级
                $result = $sysItemMultilevel->saveFourthLevel($arg_array);
                break;
        }

        echo json_encode($result);

    }


}
